<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 * 用户冻结log
 */
class UserFreezeLog extends Model
{
    protected $table = 'user_freeze_log';

    protected $fillable = [
        'user_id', 'act', 'flag', 'reason_type', 'reason', 'op_admin_id'
    ];

    const ACT_UNFREEZE = 0;
    const ACT_FREEZE = 1;
    const ACT_FORBID = 2;

    const REASON_TYPE = [
        0 => '其他',
        1 => '脚本聊天',
        2 => '广告',
        3 => '涉黄',
        4 => '恶意退款',
        5 => '涉政',
        6 => '诈骗'
    ];

    /**
     * 添加数据
     */
    static function addRecord($userId = 0, $reasonType, $act = 1, $reason = '', $adminUid = 0)
    {
        return self::create([
            'user_id' => $userId,
            'act' => $act,
            'reason_type' => $reasonType ?: 0,
            'reason' => $reason ?: '',
            'op_admin_id' => $adminUid
        ]);
    }

    /**
     * 查询冻结原因
     */
    static function checkReason($userId)
    {
        $where = [
            'user_id' => $userId,
            'act' => 1,
        ];
        $item = self::where($where)->orderBy('id', 'desc')->first();
        if (!$item) return '';
        return [
            // 'text' => self::REASON_TYPE[$item->reason_type] . '：' . $item->reason,
            'count' => self::where($where)->count()
        ];
    }
}
